package edu.my.service;

import edu.my.dao.BookDao;
import edu.my.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("bookService")
public class BookService {

    @Autowired
    BookDao bookDao;

    @Transactional
    public List<Book> findAll(){
        return bookDao.findAll();
    }

    @Transactional
    public void deleteBookByIsdn(String isdn){
        bookDao.deleteBookByIsdn(isdn);
    }

    @Transactional
    public void transfer(String sourceId, String targetId, Float money) {
        System.out.println("开始转钱了。。。");
        //转出账户
        Book source = bookDao.queryOneBookById(sourceId);
        //转入账户
        Book target = bookDao.queryOneBookById(targetId);
        //减钱
        source.setPrice(source.getPrice()-money);
        //加钱
        target.setPrice(target.getPrice()+money);
        //更新
        bookDao.modifyBook(source);
        bookDao.modifyBook(target);
    }
}
